home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: uu4news.netcom.com!zodiac!szh
- From: szh@zcon.com (Syed Zaeem Hosain)
- Subject: Re: HELP!!! Splitting strings
- Message-ID: <1996Mar22.182443.28173@zcon.com>
- Sender: szh@zcon.com (Syed Zaeem Hosain)
- Nntp-Posting-Host: zodiac
- Reply-To: szh@zcon.com
- Organization: Z Consulting Group
- References: <Pine.SUN.3.91.960320094032.5117A-100000@altair.herts.ac.uk>
- Date: Fri, 22 Mar 1996 18:24:43 GMT
-
- In article <Pine.SUN.3.91.960320094032.5117A-100000@altair.herts.ac.uk>, Wayne Rowley <cs2ff@herts.ac.uk> writes:
- >
- >Can anyone help me with this problem: I'm trying to split larger strings
- >into a number of smaller strings, for example:
- >
- > "thisisastring" to "this" "is" "a" "string"
- >
- >I can't seem to get C to do it properly. I know that C is not a good
- >language for manipulating strings, and there doesn't seem to be anything
- >in string.h to help. Does anyone have any ideas?
-
- A question I have: what is the delimiter in the source string? If it
- is *you* making this determination, rather than something like a space
- or tab, then the problem is relatively easy to do with explicit code
- like in the example I show later. If you are trying to get English
- words to be found (without delimiters) and extracted automatically,
- then you have a lot of coding ahead of you (still doable in C, of
- course) to recognize and match patterns and words!
-
- On the other hand, if you have delimiters, it is still relatively easy
- - by judicious use of the strtok() function. Of course, your comment
- about C not being a good language to manipulate strings is not correct,
- I'd say, and is likely to create a flame war, but I do not care about
- that. :-)
-
- Anyway, look at the following for a simple solution (with the simple
- assumption that *you* know where the string is to be broken apart and
- can hard-code this into the code):
-
- zodiac{158}szh: cat foo.c
- #include <stdio.h>
- #include <string.h>
-
- int main ()
- {
- char src[] = "Thisisastring";
- char w1[5] = "" , w2[3] = "" , w3[2] = "" , w4[7] = "";
-
- printf ( "Before: source = %s\nword 1 = %s\nword 2 = %s\nword 3 = %s\nword 4 = %s\n" , src , w1 , w2 , w3 , w4 );
- strncpy ( w1 , src , 4 ); w1[4] = '\0';
- strncpy ( w2 , src + 4 , 2 ); w2[2] = '\0';
- strncpy ( w3 , src + 6 , 1 ); w3[1] = '\0';
- strncpy ( w4 , src + 7 , 6 ); w4[6] = '\0';
- printf ( "After : source = %s\nword 1 = %s\nword 2 = %s\nword 3 = %s\nword 4 = %s\n" , src , w1 , w2 , w3 , w4 );
- return ( 0 );
- }
- zodiac{159}szh: gcc -o foo foo.c
- zodiac{160}szh: foo
- Before: source = Thisisastring
- word 1 =
- word 2 =
- word 3 =
- word 4 =
- After : source = Thisisastring
- word 1 = This
- word 2 = is
- word 3 = a
- word 4 = string
- zodiac{161}szh:
-
- The bottom line being that there is nothing tough about using strncpy.
- And this can be found in string.h, so you need to look at your library
- reference manual more carefully for string manipulation functions ...
- they *do* exist!
-
- Z
-
-
- --
- -------------------------------------------------------------------------
- | Syed Zaeem Hosain P. O. Box 610097 (408) 441-7021 |
- | Z Consulting Group San Jose, CA 95161 szh@zcon.com |
- -------------------------------------------------------------------------
-